package org.jef.socket.config;

import org.jef.socket.event.EventType;
import org.jef.socket.websocket.JefRedisReceiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @version 1.0
 * @description: 配置redis转发
 * @author: ChengZhi 0336
 * @create: 2024-09-09 17:55
 **/
@Configuration
public class WebSocketRedisConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 可以添加多个 messageListener，配置不同的交换机
        container.addMessageListener(listenerAdapter, new PatternTopic(EventType.TOPIC_WEBSOCKET.getValue()));// 订阅最新消息频道
        container.addMessageListener(listenerAdapter, new PatternTopic(EventType.TOPIC_COUNTDOWN.getValue()));// 订阅最新消息频道
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(JefRedisReceiver receiver) {
        // 消息监听适配器
        return new MessageListenerAdapter(receiver, "onMessage");
    }
}
